**------------------------------------------
** Replication materials: "Elite Cues and Economic Policy Attitudes: The Mediating Role of Economic Hardship" by Charlotte Cavaille and Anja Neundorf


* Download BHPS data: You need to download the data "British Household Panel Survey: Waves 1-18, 1991-2009" from: https://beta.ukdataservice.ac.uk/datacatalogue/studies/study?id=5151#!/details

** Mergin BHPS data

clear all
set more off
cd "/Users/aneundorf/Dropbox/Data/BHPS" // SAVE BHPS DATA AND SAVE IN DIRECTORY, WHICH SHOULD BE ADDED HERE.

* ========================================
* = extract variables from raw bhps data =
* ========================================


local waves = "a b c d e f g h i j k l m n o p q r"

foreach i of local waves {
	use "original/`i'indresp", clear
	keep `i'vote1 `i'vote2 `i'vote4 `i'vote5 `i'sex `i'qfachi `i'casmin `i'isced `i'jbgold `i'tenure `i'region ///
	`i'race  `i'qfedhi `i'fimn `i'fiyr `i'fiyrl `i'fiyeari `i'jbonus `i'jbrise `i'age `i'hid `i'pno  `i'jbsemp ///
	`i'jbmngr `i'jbisco `i'jssize  `i'jbhrs `i'jbot `i'jshrs `i'jbsoc `i'jbsect ///
	`i'paygu `i'paynu `i'paygui `i'paynui `i'mastat  ///
	`i'fimnb `i'fimnbi `i'fimnp `i'fimnpi   ///
	`i'fiyrb `i'fiyrbi `i'fiyrp `i'fiyrpi   `i'fiyrt `i'fiyrti  ///
	`i'hlsv `i'jbsat4 `i'plbornc  `i'jbft `i'jbstat  `i'jbub `i'jbuby `i'fisit `i'fisitc `i'fisitx  ///
	`i'jbstatl `i'jbstat `i'spjbyr `i'cjsbgy4 `i'jlend4 `i'njbnew `i'njbwks `i'sppid `i'jboff `i'jboffy pid
	gen wave = "`i'"
	sort pid
	renpfix `i'
	save "_`i'", replace
}
* ip -- pid
use "_p"
ren id pid
save "_p", replace

use _a
append using _b _c _d _e _f _g _h _i _j _k _l _m _n _o _p _q _r
sort pid wave

*gen numeric wave indicator
egen waven = group(wave)
foreach i of local waves {
erase _`i'.dta
}

save "temp/__main.dta", replace

* ========================================
* = extract variables vote3 =
* ========================================


local waves = "a c d e f g h i j k l m n o p q r"

foreach i of local waves {
	use "original/`i'indresp"
	keep `i'vote3  pid 
	gen wave = "`i'"
	sort pid
	renpfix `i'
	save "_`i'", replace
}
* ip -- pid
use "_p"
ren id pid
save "_p", replace

use _a
append using   _c _d _e _f _g _h _i _j _k _l _m _n _o _p _q _r
sort pid wave

*gen numeric wave indicator
egen waven = group(wave)
foreach i of local waves {
erase _`i'.dta
}

save "temp/__main_vote3.dta", replace


* ========================================
* = extract variables Fin sit change =
* ========================================


local waves = "c d e f g h i j k l m n o p q r"

foreach i of local waves {
	use "original/`i'indresp"
	keep `i'fisitc `i'fisity pid 
	gen wave = "`i'"
	sort pid
	renpfix `i'
	save "_`i'", replace
}
* ip -- pid
use "_p"
ren id pid
save "_p", replace

use _c
append using _d _e _f _g _h _i _j _k _l _m _n _o _p _q _r
sort pid wave

*gen numeric wave indicator
egen waven = group(wave)
foreach i of local waves {
erase _`i'.dta
}

save "temp/__main_fisit.dta", replace

* ========================================
* = extract vote variables =
* ========================================


local waves = " b e g h i j k l m n o p q r"

foreach i of local waves {
	use "original/`i'indresp"
	keep `i'vote1 `i'vote7 `i'vote8  pid 
	gen wave = "`i'"
	sort pid
	renpfix `i'
	save "temp/_`i'vot", replace
}
* ip -- pid
use temp/_pvot
ren id pid
save "temp/_pvot", replace

use temp/_bvot
append using temp/_evot temp/_gvot temp/_hvot temp/_ivot temp/_jvot temp/_kvot temp/_lvot temp/_mvot temp/_nvot ///
 temp/_ovot temp/_pvot temp/_qvot temp/_rvot
sort pid wave

*gen numeric wave indicator
egen waven = group(wave)
foreach i of local waves {
erase "temp/_`i'vot.dta"
}

save "temp/__mainvote", replace



* ========================================
* = extract political interest  =
* ========================================

local waves = "a b c d e f  k l m n o p q r"

foreach i of local waves {
	use "original/`i'indresp"
	keep `i'vote6  pid 
	gen wave = "`i'"
	sort pid
	renpfix `i'
	save "_`i'pol", replace
}
* ip -- pid
use _ppol
ren id pid
save "_ppol", replace

use _apol
append using _bpol _cpol _dpol _epol _fpol _kpol _lpol _mpol _npol _opol _ppol _qpol _rpol
sort pid wave

*gen numeric wave indicator
egen waven = group(wave)
save temp/__main_polint, replace

foreach i of local waves {
erase "_`i'pol.dta"
}


* ========================================
* = extract pol. ideology variables  =
* ========================================


local waves = "a c e g j n q"

foreach i of local waves {
	use "original/`i'indresp"
	keep `i'opsoca `i'opsocb `i'opsocc `i'opsocd `i'opsoce `i'opsocf  pid 
	gen wave = "`i'"
	sort pid
	renpfix `i'
	save _`i'ido, replace
}


use _aido
append using  _cido _eido _gido _jido _nido _qido 
sort pid wave

*gen numeric wave indicator
egen waven = group(wave)

save "temp/__mainido", replace

foreach i of local waves {
erase "_`i'ido.dta"
}


* ============================
* = merge in additional info =
* ============================

use "original/xwlsten"
sort pid
save __tmp2, replace

use "temp/__main", clear
*merge m:1 pid using __tmp1, gen(_merge1)
merge m:1 pid using __tmp2, gen(_merge2)
sort pid wave

merge pid wave using "temp/__main_polint"
sort pid wave
drop _merge

merge pid wave using "temp/__mainvote"
sort pid wave
drop _merge

merge pid wave using "temp/__main_vote3"
sort pid wave
drop _merge


merge pid wave  using "temp/__main_fisit"
sort pid wave
drop _merge

merge pid wave  using "temp/__mainido"
sort pid wave
drop _merge

drop _merge2

sort pid wave
browse pid waven


* ===========================
* = ----- CHECK ------ =
* ===========================
rename waven time
sort hid pid time

browse hid pid time vote8

***** SAFE
compress
saveold "/Users/aneundorf/Dropbox/Project_CavailleNeundorf/Paper1/Analysis/BHPS_working", replace // CHANGE DIRECTORY HERE TO WHERE YOU WANT TO SAVE YOUR WORKING FILE.

*log close

